package com.quanquan.management.controller;


import DTO.PageDTO;
import com.alibaba.fastjson.JSONArray;
import com.quanquan.management.dto.SignInDTO;
import com.quanquan.management.dto.UserDTO;
import com.quanquan.management.request.UserRequestBody;
import com.quanquan.management.service.UserService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import request.PageRequestBody;
import utils.MobileUtil;
import utils.StringUtil;
import web.response.ResultBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author gongzhou
 * @title: UserController
 * @projectName SpringCloud
 * @description: TODO
 * @date 2019/3/2810:23
 */

@Controller
@RequestMapping("/user")

public class UserController extends ManagerBaseController {

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       @RequestParam(required = false) Integer fromInt,
                       @RequestParam(required = false) Integer toInt, Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        UserRequestBody requestBody = new UserRequestBody();
        requestBody.setPage(page);
        requestBody.setSize(pageSize);
        requestBody.setKeyword(keyword);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setFromInt(fromInt);
        requestBody.setToInt(toInt);
        PageDTO<UserDTO> pageDTO = userService.list(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "user/list";
    }

    @RequestMapping("/signIn/list")
    public String signInList(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate, Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        PageRequestBody requestBody = new PageRequestBody();
        requestBody.setPage(page);
        requestBody.setSize(pageSize);
        requestBody.setKeyword(keyword);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        PageDTO<SignInDTO> pageDTO = userService.signInPageList(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "user/signIn_list";
    }

    @RequestMapping("/search")
    @ResponseBody
    public PageDTO<UserDTO> search(@RequestParam(required = false) Integer pageSize,
                                   @RequestParam(required = false) Integer page,
                                   @RequestParam(required = false) String keyword) {
        if (null == pageSize) {
            pageSize = 5;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        UserRequestBody requestBody = new UserRequestBody();
        requestBody.setSize(pageSize);
        requestBody.setPage(page);
        requestBody.setKeyword(keyword);
        PageDTO<UserDTO> pageDTO = userService.list(requestBody);
        return pageDTO;
    }

    @PostMapping("/table_list")
    public @ResponseBody Map<String, Object> tableList(UserRequestBody requestBody) {
        Integer page = requestBody.getPage();
        if (null != page && page > 0) {
            page = page - 1;
            requestBody.setPage(page);
        }
        PageDTO<UserDTO> pageDTO = userService.list(requestBody);
        Map<String, Object> data = new HashMap<>();
        if (null != pageDTO) {
            data.put("size",pageDTO.getPageSize());
            data.put("rows",pageDTO.getList());
            data.put("status",200);
            data.put("msg","success!");
            data.put("total",pageDTO.getTotal());
        } else {
            data.put("size",10);
            data.put("rows",null);
            data.put("status",-200);
            data.put("msg","no data!");
            data.put("total",0);
        }
        return data;
    }

    @PostMapping("/integral")
    @ResponseBody
    public ResultBody updateIntegral(String data) {
        JSONArray array = JSONArray.parseArray(data);
        List<UserRequestBody> list = array.toJavaList(UserRequestBody.class);
        try {
            list.forEach(request -> userService.update(request));
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
    }

    @PostMapping("/create")
    @ResponseBody
    public ResultBody register(UserRequestBody requestBody) {
        String phone = requestBody.getPhone();
        if (StringUtil.isEmpty(requestBody.getPhone())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "手机号不能为空");
        }
        if (!MobileUtil.isPhoneLegal(phone)) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "手机号不合法");
        }
        return userService.create(requestBody);
    }

    @GetMapping("/get")
    @ResponseBody
    public ResultBody getById(@RequestParam String id) {
        if (StringUtil.isEmpty(id)) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        UserDTO byId = userService.findById(id);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), byId);
    }

    @PostMapping("/update")
    @ResponseBody
    public ResultBody update(UserRequestBody requestBody) {
        try {
            boolean update = userService.update(requestBody);
            if (update) {
                return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
            } else {
                return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
            }
        } catch (Exception e) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
    }

    @GetMapping("/freeze")
    @ResponseBody
    public ResultBody freeze(@RequestParam String id) {
        if (StringUtil.isEmpty(id)) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        UserDTO userDTO = userService.freeze(id);
        if (userDTO != null) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), userDTO);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }

    @RequestMapping("/export")
    public ResultBody exportData(@RequestParam(required = false) String keyword,
                                 @RequestParam(required = false) String fromDate,
                                 @RequestParam(required = false) String toDate,
                                 @RequestParam(required = false) Integer fromInt,
                                 @RequestParam(required = false) Integer toInt,
                                 @RequestParam(required = false) Integer total) {

        UserRequestBody requestBody = new UserRequestBody();
        requestBody.setPage(0);
        requestBody.setSize(total);
        requestBody.setKeyword(keyword);
        requestBody.setFromDate(fromDate);
        requestBody.setFromInt(fromInt);
        requestBody.setToInt(toInt);
        requestBody.setToDate(toDate);
        try {
            userService.export(this.response, requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
    }

    @RequestMapping("/signIn/export")
    public ResultBody exportSignInData(@RequestParam(required = false) String keyword,
                                 @RequestParam(required = false) String fromDate,
                                 @RequestParam(required = false) String toDate,
                                 @RequestParam(required = false) Integer total) {
        PageRequestBody requestBody = new PageRequestBody();
        requestBody.setPage(0);
        requestBody.setSize(total);
        requestBody.setKeyword(keyword);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        try {
            userService.exportSign(this.response, requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
    }

}
